home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-10-12 | 3.8 KB | 156 lines |
- package com.symantec.itools.swing;
-
-
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.LayoutManager;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Enumeration;
- import java.util.Vector;
- import com.sun.java.swing.AbstractButton;
- import com.sun.java.swing.ButtonGroup;
- import com.sun.java.swing.ButtonModel;
- import com.sun.java.swing.JPanel;
-
-
- public class JButtonGroupPanel
- extends JPanel
- implements ActionListener
- {
- protected Vector buttons;
- protected Vector listeners;
- protected ButtonGroup group;
-
- {
- buttons = new Vector();
- group = new ButtonGroup();
- listeners = new Vector();
- }
-
- public JButtonGroupPanel()
- {
- super.setLayout(new GridLayout());
- }
-
- public JButtonGroupPanel(ButtonGroup grp)
- {
- for (Enumeration e = grp.getElements(); e.hasMoreElements();)
- {
- add((AbstractButton)e.nextElement());
- }
- }
-
- public Enumeration getElements()
- {
- return (group.getElements());
- }
-
- public void addActionListener(ActionListener listener)
- {
- if (!(listeners.contains(listener)))
- listeners.addElement(listener);
- }
-
- public void removeActionListener(ActionListener listener)
- {
- if (listeners.contains(listener))
- listeners.removeElement(listener);
- }
-
- public void actionPerformed(ActionEvent event)
- {
- Vector vector;
- ActionEvent evt;
-
- synchronized(listeners)
- {
- vector = (Vector)listeners.clone();
- }
-
- evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ((AbstractButton)event.getSource()).getText());
-
- for(Enumeration e = vector.elements(); e.hasMoreElements();)
- {
- ((ActionListener)e.nextElement()).actionPerformed(evt);
- }
- }
-
- protected void addImpl(Component comp, Object constraints, int index)
- {
- if (comp instanceof AbstractButton)
- {
- AbstractButton btn = (AbstractButton)comp;
-
- if (!(buttons.contains(btn)))
- {
- buttons.addElement(btn);
- group.add(btn);
- btn.addActionListener(this);
- super.addImpl(comp, constraints, index);
- }
- }
- else super.addImpl(comp, constraints, index);
- }
-
- public void remove(AbstractButton btn)
- {
- if(buttons.contains(btn))
- {
- buttons.removeElement(btn);
- group.remove(btn);
- }
- }
-
- public void setSelection(AbstractButton btn, boolean f)
- {
- btn.getModel().setSelected(f);
- }
-
- public void setSelection(String text, boolean f)
- {
- for(Enumeration e = buttons.elements(); e.hasMoreElements();)
- {
- AbstractButton btn;
-
- btn = (AbstractButton)e.nextElement();
-
- if(btn.getText().equals(text))
- {
- setSelection(btn, f);
- }
- }
- }
-
- public AbstractButton getSelection()
- {
- ButtonModel model;
-
- model = group.getSelection();
-
- for(Enumeration e = buttons.elements(); e.hasMoreElements();)
- {
- AbstractButton btn;
-
- btn = (AbstractButton)e.nextElement();
-
- if(btn.getModel() == model)
- {
- return (btn);
- }
- }
-
- return (null);
- }
-
- public boolean isSelected(AbstractButton btn)
- {
- return (btn == getSelection());
- }
-
- public boolean isSelected(String text)
- {
- return (text.equals(getSelection().getText()));
- }
- }